﻿
@{
    ViewBag.Title = "Radiologist Index";
}

@Html.Partial("_UserPage")
<div class="row searchbar">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bar">@Html.Partial("_SearchPatient")</div>
</div>
<div id="patient" class="row result">
    <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 sidebar">@Html.Partial("_PatientInfo")</div>
    <div class="col-xs-9 col-sm-9 col-md-10 col-lg-10 main">@Html.Partial("~/Views/Radiologist/ListOfIndicationForRadiologist.cshtml")</div>
</div>
@Html.Partial("_Message")
@Html.Partial("~/Views/Radiologist/UpdateIndication.cshtml")
@Html.Partial("~/Views/Radiologist/ListOfImageVisit.cshtml")

@section Scripts {
    @Scripts.Render("~/bundles/message")
    @Scripts.Render("~/Scripts/BodyController.js")
    @Scripts.Render("~/Scripts/ListHeightController.js")
    @Scripts.Render("~/bundles/kinetic")
    @Scripts.Render("~/bundles/knockout")
    @Scripts.Render("~/bundles/my_knockout")
    @Scripts.Render("~/Scripts/ViewModel/PatientVM_Radiologist.js")
    @Scripts.Render("~/Scripts/ViewModel/IndicationUpdateVM.js")
    @Scripts.Render("~/Scripts/ImageController.js")
    @Scripts.Render("~/Scripts/ViewModel/ListImageVisitVM.js")
    <script>
        $(function () {
            /*Binding Patient Information for index page*/
            ko.applyBindings(patientVM_Radiologist, document.getElementById("patient"));

            /*Binding Update Indication Info*/
            ko.applyBindings(indicationUpdateVM, document.getElementById("indicationvm"));

            /*Binding List ImageVisits*/
            ko.applyBindings(imageVisitsVM, document.getElementById("imagevisitsvm"));//$("#abc") not run
            var indexController = {
                searchTerm: $("#searchTerm"),
                btnSearch: $("#btnSearch"),
                //enableBtnSearch: function (state) {
                //},
                enableSearch: function (state) {
                    var p = this;
                    if (state == true) {
                        this.btnSearch.on("click", function () {
                            //alert();
                            var searchType;
                            if ($("#searchPatientID").prop("checked") == true) {
                                searchType = 1;
                            }
                            else {
                                searchType = 2;
                            }
                            //alert(JSON.stringify({ id: $("#searchTerm").prop("value"), type: searchType }));
                            patientVM_Radiologist.loadPatientInfo(p.searchTerm.prop("value"), searchType, indexController);
                        });
                        this.searchTerm.on("keypress", function (event) {
                            if (event.which == 13) {//Enter
                                p.btnSearch.trigger("click");
                            }
                        });
                    }
                    else {
                        p.btnSearch.off("click");
                        p.searchTerm.off("keypress");
                    }
                },
                message: messageController,
                body: bodyController,

                updateIndication: $("#update-indication"),
                displayUpdateIndication: function (state) {
                    if (state == true) {
                        this.updateIndication.css({
                            "display": "block"
                        });
                        this.body.displayBodyScroll(false);
                    }
                    else {
                        this.updateIndication.css({
                            "display": "none"
                        });
                        this.body.displayBodyScroll(true);
                    }
                },
                btnUpdateIndicationSave: $("#update-indication-save"),
                enableBtnUpdateIndicationSave: function(state) {
                    if (state == true) {
                        this.btnUpdateIndicationSave.on("click", function () {
                            bodyController.stopRequests();//stop download images
                            indicationUpdateVM.saveIndication(indexController);
                        });
                    }
                    else {
                        this.btnUpdateIndicationSave.off("click");
                    }
                },
                btnUpdateIndicationCancel: $("#update-indication-cancel"),
                enableBtnUpdateIndicationCancel: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnUpdateIndicationCancel.on("click", function () {
                            self.displayUpdateIndication(false);
                            indicationUpdateVM.cancelLoadRequest();//stop ajax
                            bodyController.stopRequests();//stop download images
                        });
                    }
                    else {
                        self.btnUpdateIndicationCancel.off("click");
                    }
                },

                btnFirstIndicationPage: $("#firstIndicationPage"),
                btnPreviousIndicationPage: $("#previousIndicationPage"),
                enableIndicationPreviousGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnFirstIndicationPage.on("click", function () {
                            patientVM_Radiologist.loadIndicationsPage(indexController, 1);
                        });
                        self.btnFirstIndicationPage.removeClass("disabled");
                        self.btnPreviousIndicationPage.on("click", function () {
                            patientVM_Radiologist.loadIndicationsPage(indexController, patientVM_Radiologist.IndicationsPage() - 1);
                        });
                        self.btnPreviousIndicationPage.removeClass("disabled");
                    }
                    else {
                        self.btnFirstIndicationPage.off("click");
                        self.btnFirstIndicationPage.addClass("disabled");
                        self.btnPreviousIndicationPage.off("click");
                        self.btnPreviousIndicationPage.addClass("disabled");
                    }
                },
                btnLastIndicationPage: $("#lastIndicationPage"),
                btnNextIndicationPage: $("#nextIndicationPage"),
                enableIndicationNextGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnLastIndicationPage.on("click", function () {
                            patientVM_Radiologist.loadIndicationsPage(indexController, patientVM_Radiologist.IndicationsPageCount());
                        });
                        self.btnLastIndicationPage.removeClass("disabled");
                        self.btnNextIndicationPage.on("click", function () {
                            patientVM_Radiologist.loadIndicationsPage(indexController, patientVM_Radiologist.IndicationsPage() + 1);
                        });
                        self.btnNextIndicationPage.removeClass("disabled");
                    }
                    else {
                        self.btnLastIndicationPage.off("click");
                        self.btnLastIndicationPage.addClass("disabled");
                        self.btnNextIndicationPage.off("click");
                        self.btnNextIndicationPage.addClass("disabled");
                    }
                },

                listIndication: $("#listIndication"),
                enableListIndication: function (state) {
                    var self = this;
                    if (state == true) {
                        self.listIndication.on("click", "tr", function (event) {
                            indicationUpdateVM.loadIndicationInfo($(event.currentTarget).find("td").eq(0).text(), imageController, indexController);
                            self.displayUpdateIndication(true);
                        });
                        self.enableBtnUpdateIndicationCancel(true);
                        /*Not run*/
                        //var rows = $("#listIndication");
                        //rows.on("click", function () {
                        //    indicationUpdateVM.loadIndicationInfo($(this).first("td").text());
                        //    $("#update-indication").css({
                        //        "display": "block"
                        //    });
                        //});
                    }
                    else {
                        self.listIndication.off("click");
                        self.enableBtnUpdateIndicationCancel(false);
                    }
                },

                enableUpdateIndicationTool: function (state) {
                    var self = this;
                    if (state == true) {
                        $("#showListImageVisit").on("click", function () {
                            imageVisitsVM.loadImageVisitsPage(patientVM_Radiologist.Patient()[0].ID, indexController, 1);
                            self.displayListImageVisitForm(true);
                        });
                        $("#list-imageVisit-close").on("click", function () {
                            self.displayListImageVisitForm(false);
                            imageVisitsVM.cancelLoadImageVisitsPageRe();//cancel request
                        });
                    }
                    else {
                        $("#showListImageVisit").off("click");
                        $("#list-imageVisit-close").off("click");
                    }
                },

                listImageVisitForm: $("#list-imageVisit"),
                displayListImageVisitForm: function (state) {
                    if (state == true) {
                        this.listImageVisitForm.show();
                        listHeightController.updateListImageVisitHeight();
                    }
                    else {
                        this.listImageVisitForm.hide();
                    }
                },
                btnFirstImageVisitPage: $("#firstImageVisitPage"),
                btnPreviousImageVisitPage: $("#previousImageVisitPage"),
                enableImageVisitPreviousGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnFirstImageVisitPage.on("click", function () {
                            imageVisitsVM.loadImageVisitsPage(patientVM_Radiologist.Patient()[0].ID, indexController, 1);
                        });
                        self.btnFirstImageVisitPage.removeClass("disabled");
                        self.btnPreviousImageVisitPage.on("click", function () {
                            imageVisitsVM.loadImageVisitsPage(patientVM_Radiologist.Patient()[0].ID, indexController, imageVisitsVM.ImageVisitsPage() - 1);
                        });
                        self.btnPreviousImageVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnFirstImageVisitPage.off("click");
                        self.btnFirstImageVisitPage.addClass("disabled");
                        self.btnPreviousImageVisitPage.off("click");
                        self.btnPreviousImageVisitPage.addClass("disabled");
                    }
                },
                btnLastImageVisitPage: $("#lastImageVisitPage"),
                btnNextImageVisitPage: $("#nextImageVisitPage"),
                enableImageVisitNextGroup: function (state) {
                    var self = this;
                    if (state == true) {
                        self.btnLastImageVisitPage.on("click", function () {
                            imageVisitsVM.loadImageVisitsPage(patientVM_Radiologist.Patient()[0].ID, indexController, imageVisitsVM.ImageVisitsPageCount());
                        });
                        self.btnLastImageVisitPage.removeClass("disabled");
                        self.btnNextImageVisitPage.on("click", function () {
                            imageVisitsVM.loadImageVisitsPage(patientVM_Radiologist.Patient()[0].ID, indexController, imageVisitsVM.ImageVisitsPage() + 1);
                        });
                        self.btnNextImageVisitPage.removeClass("disabled");
                    }
                    else {
                        self.btnLastImageVisitPage.off("click");
                        self.btnLastImageVisitPage.addClass("disabled");
                        self.btnNextImageVisitPage.off("click");
                        self.btnNextImageVisitPage.addClass("disabled");
                    }
                },

                listImageVisit: $("#listImageVisit"),
                enableListImageVisit: function (state) {
                    var self = this;
                    if (state == true) {
                        self.listImageVisit.on("click", "tr", function (event) {
                            //close and stop all requests
                            self.displayListImageVisitForm(false);
                            //stop download images
                            bodyController.stopRequests();
                            //reset, clear old data, construct, disable event handling
                            imageController.clearOldData();
                            //can make new request
                            var imageVisitID = $(event.currentTarget).find("td").eq(0).text();
                            imageController.loadImages(patientVM_Radiologist.Patient()[0].ID, imageVisitID, 0);
                            ////not update viewmodel
                            //$("#ImageVisitID").prop({
                            //    "value": imageVisitID
                            //});
                            //$("#ImageID").prop({
                            //    "value": 0
                            //});
                            ////not update view
                            //indicationUpdateVM.UpdateIndication()[0].ImageVisitID = imageVisitID;
                            //indicationUpdateVM.UpdateIndication()[0].ImageID = 0;
                            indicationUpdateVM.ImageVisitID(imageVisitID);//indicationUpdateVM.ImageVisitID = imageVisitID;// not update view
                            indicationUpdateVM.ImageID(0);
                        });
                    }
                    else {
                        self.listImageVisit.off("click");
                    }
                }
            };
            //indexController.enableBtnSearch(true);
            indexController.enableSearch(true);
            indexController.searchTerm.focus();
            //update listImageVisit height - only radiologist has
            //$(window).on("resize", function () {
            //    listHeightController.updateListImageVisitHeight();
            //});
            indexController.enableIndicationPreviousGroup(false);
            indexController.enableIndicationNextGroup(false);
        });
    </script>
}